package com.webinsky.web.resource;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;

import com.webinsky.article.domain.MisRelationship;
import com.webinsky.article.domain.MisRelationshipSync;
import com.webinsky.article.domain.MisRelationshipType;
import com.webinsky.article.service.ArticleManagement;
import com.webinsky.article.service.RelationshipManagement;
import com.webinsky.common.proj.Lang;
import com.webinsky.domain.catalog.Catalog;
import com.webinsky.domain.catalog.Catalogs;
import com.webinsky.domain.catalog.Tag;
import com.webinsky.domain.catalog.Tags;
import com.webinsky.domain.resource.ResType;
import com.webinsky.domain.resource.Resource;
import com.webinsky.folder.domain.FolderRelationshipSync;
import com.webinsky.folder.domain.FolderType;
import com.webinsky.folder.domain.Folders;
import com.webinsky.folder.domain.FolderRelationshipSync.FolderRelationship;
import com.webinsky.folder.service.FolderManagement;
import com.webinsky.service.catalog.CatalogManagement;
import com.webinsky.service.catalog.TagManagement;
import com.webinsky.service.resource.ResManagement;
import com.webinsky.web.data.DeleteCatalogForm;
import com.webinsky.web.data.DeleteLangCatalogForm;
import com.webinsky.web.data.DeleteLangTagForm;
import com.webinsky.web.data.DeleteTagForm;
import com.webinsky.web.data.ItemForm;
import com.webinsky.web.data.ItemsForm;
import com.webinsky.web.data.NewCatalogForm;
import com.webinsky.web.data.NewCatalogForms;
import com.webinsky.web.data.NewTagForm;
import com.webinsky.web.data.NewTagForms;
import com.webinsky.web.data.Response;
import com.webinsky.web.data.SearchCatalogForm;
import com.webinsky.web.data.SearchLangCatalogForm;
import com.webinsky.web.data.SearchLangTagForm;
import com.webinsky.web.data.SearchTagForm;
import com.webinsky.web.data.SyncTagForm;
import com.webinsky.web.data.UpdateCatalogForm;
import com.webinsky.web.data.UpdateTagForm;
import com.webinsky.folder.domain.Folder;
import com.webinsky.folder.domain.FolderFilter;

import reactor.core.publisher.Mono;


@Component
public class CatalogResource {

	private static final Logger logger = 
			LoggerFactory.getLogger(CatalogResource.class);
	@Autowired
	private ArticleManagement articleManagement;
	@Autowired
	private CatalogManagement catalogManagement;
	@Autowired
	private TagManagement tagManagement;
	@Autowired
	private RelationshipManagement relationShipMangement;
	@Autowired
	private FolderManagement folderManagement;
	@Autowired
	private RelationshipManagement relationshipManagement;
	@Autowired
	private ResManagement resManagement;
	
	///////////////////////////////////////////////////////////////////////
	// catalog
	/**
	 * 新建目录
	 * @param req
	 * @return
	 */
	public Mono<ServerResponse> createCatalog(ServerRequest req){
		return req.bodyToMono(NewCatalogForm.class).flatMap(form -> {
			Catalog catalog = new Catalog();
			catalog.setName(form.name);
			catalog.setLang(Lang.parse(form.lang));
			catalog.setSiteId(Integer.valueOf(form.siteId));
			catalogManagement.create(catalog);
			return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).syncBody(Response.create(catalog, Response.STATUS_OK, ""));
		});
	}
	/**
	 * 更新目录
	 * @param req
	 * @return
	 */
	public Mono<ServerResponse> updateCatalog(ServerRequest req){
		return req.bodyToMono(UpdateCatalogForm.class).flatMap(form -> {
			catalogManagement.updateName(form.id, form.name);
			return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).syncBody(Response.create("", Response.STATUS_OK, ""));
		});
	}
	/**
	 * 删除目录
	 * @param req
	 * @return
	 */
	public Mono<ServerResponse> deleteCatalog(ServerRequest req){
		return req.bodyToMono(DeleteCatalogForm.class).flatMap(form -> {
			catalogManagement.delete(form.id);
			return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).syncBody(Response.create("", Response.STATUS_OK, ""));
		});
	}
	/**
	 * 分页取得目录
	 * @param req
	 * @return
	 */
	public Mono<ServerResponse> searchCatalogs(ServerRequest req){
		return req.bodyToMono(SearchCatalogForm.class).flatMap(form -> {
			int siteId = Integer.valueOf(form.siteId);
			Lang lang = Lang.parse(form.lang);
			Catalogs catalogs = catalogManagement.getCatalogByPagnation(siteId, lang, form.start, form.size);
			return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).syncBody(Response.create(catalogs, Response.STATUS_OK, ""));
		});
	}
	///////////////////////////////////////////////////////////////////////
	// tag 
	/**
	 * 新建Tag
	 * @param req
	 * @return
	 */
	public Mono<ServerResponse> createTag(ServerRequest req){
		return req.bodyToMono(NewTagForm.class).flatMap(form -> {
			Tag tag = new Tag();
			tag.setName(form.tag);
			tag.setLang(Lang.parse(form.lang));
			Catalog catalog = new Catalog();
			catalog.setId(form.tagcatId);
			catalog.setLang(Lang.parse(form.lang));
			catalog.setSiteId(Integer.valueOf(form.siteId));
			tag.setCatalog(catalog);
			tagManagement.create(tag);
			return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).syncBody(Response.create(tag.getId(), Response.STATUS_OK, ""));
		});
	}
	/**
	 * 更新Tag
	 * @param req
	 * @return
	 */
	public Mono<ServerResponse> updateTag(ServerRequest req){
		return req.bodyToMono(UpdateTagForm.class).flatMap(form -> {
			Tag tag = new Tag();
			tag.setName(form.name);
			tag.setId(form.id);
			tagManagement.update(tag);
			return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).syncBody(Response.create("", Response.STATUS_OK, ""));
		});
	}
	/**
	 * 删除tag
	 * @param req
	 * @return
	 */
	public Mono<ServerResponse> deleteTag(ServerRequest req) {
		return req.bodyToMono(DeleteTagForm.class).flatMap(form -> {
			tagManagement.delete(form.id);
			return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).syncBody(Response.create("", Response.STATUS_OK, ""));
		});
	}
	/**
	 * 分页tag查找
	 * @param req
	 * @return
	 */
	public Mono<ServerResponse> searchTag(ServerRequest req) {
		return req.bodyToMono(SearchTagForm.class).flatMap(form -> {
			Integer siteId = Integer.valueOf(form.siteId);
			Lang lang = Lang.parse(form.lang);
			Tags tags = tagManagement.getTagsByName(form.catalogId, form.name, siteId, lang, form.start, form.size);
			return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).syncBody(Response.create(tags, Response.STATUS_OK, ""));
		});
	}
	/**
	 * 批量新建标签分类
	 * 
	 * @param req
	 * @return
	 */
	public Mono<ServerResponse> batchCreateCatalogs(ServerRequest req) {
		return req.bodyToMono(NewCatalogForms.class).flatMap(data -> {
			if ( data != null && data.forms != null && !data.forms.isEmpty() ) {
				// 检查是更新还是新增
				List<Catalog> catalogs = new ArrayList<>();
				boolean isUpdate = false;
				for ( NewCatalogForm f : data.forms ){
					if ( f.id > 0 ){
						isUpdate = true;
					}
					Catalog catalog = new Catalog();
					catalog.setId(f.id);
					catalog.setName(f.name);
					catalog.setLang(Lang.parse(f.lang));
					catalog.setSiteId(Integer.valueOf(data.siteId));
					catalogs.add(catalog);
				}
				if ( isUpdate ){
					catalogManagement.update(catalogs);
					return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).syncBody(Response.create(catalogs, Response.STATUS_OK, ""));
				}
				catalogManagement.create(catalogs);
				// 新建的时候需要记录relationship
				MisRelationship misRelationship = new MisRelationship();
				misRelationship.setSiteId(Integer.valueOf(data.siteId));
				misRelationship.setType(MisRelationshipType.Tagcat);
				catalogs.forEach(catalog -> {
					switch ( catalog.getLang() ) {
					case jaJP:
						misRelationship.setJpId(catalog.getId());
						break;
					case zhCN:
						misRelationship.setCnId(catalog.getId());
						break;
					case enUS:
						misRelationship.setEnId(catalog.getId());
						break;
					case zhTW:
						misRelationship.setTwId(catalog.getId());
						break;
					case ko:
						misRelationship.setKrId(catalog.getId());
						break;
					case th:
						misRelationship.setThId(catalog.getId());
						break;
					}
				});
				relationShipMangement.create(misRelationship);
				return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).syncBody(Response.create(catalogs, Response.STATUS_OK, ""));
			}
			return ServerResponse.status(HttpStatus.BAD_REQUEST).contentType(MediaType.APPLICATION_JSON).syncBody(Response.create("", Response.STATUS_WRONGARUGS_ERROR, ""));
		});
	}


	/**
	 * 获取多语言标签分类
	 * @param req
	 * @return
	 */
	public Mono<ServerResponse> searchCatalog(ServerRequest req){
		return req.bodyToMono(SearchLangCatalogForm.class).flatMap(form -> {
			List<Catalog> catalogs = new ArrayList<>();
			Integer siteId = Integer.valueOf(form.siteId);
			if ( siteId != null ){
				List<MisRelationship> relationships = relationshipManagement.getRelationshipBySiteIdAndType(siteId, MisRelationshipType.Tagcat);
				relationships.forEach(e -> {
					if ( e.getCnId() == form.id ){
						List<Integer> ids = new ArrayList<>();
						ids.add(e.getJpId());
						ids.add(e.getEnId());
						ids.add(e.getTwId());
						ids.add(e.getThId());
						ids.add(e.getKrId());
						ids.add(e.getCnId());
						Catalogs c = catalogManagement.getCatalogsByIds(siteId, ids);
						catalogs.addAll(c.getCatalogs());
					}
				});
			}
			return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).syncBody(Response.create(catalogs, Response.STATUS_OK, ""));
		});
	}
	/**
	 * 删除多语言标签分类
	 * @param req
	 * @return
	 */
	public Mono<ServerResponse> delCatalog(ServerRequest req){
		return req.bodyToMono(DeleteLangCatalogForm.class).flatMap(form -> {
			List<Integer> ids = form.id;
			ids.forEach(i -> {
				catalogManagement.delete(i);				
			});
			return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).syncBody(Response.create("", Response.STATUS_OK, ""));
		});
	}
	
	/**
	 * @param req
	 * @return
	 */
	public Mono<ServerResponse> batchCreateTags(ServerRequest req) {
		return req.bodyToMono(NewTagForms.class).flatMap(data -> {
			if ( data != null && data.forms != null && !data.forms.isEmpty() ) {
				Integer siteId = Integer.valueOf(data.siteId);
				List<MisRelationship> tagcatRelationships = relationShipMangement.getRelationshipBySiteIdAndType(siteId, MisRelationshipType.Tagcat);
				if ( tagcatRelationships.isEmpty() ){
					return ServerResponse.badRequest().contentType(MediaType.APPLICATION_JSON).syncBody(Response.create("", Response.STATUS_WRONGARUGS_ERROR, "bookMode = " + data.siteId + "找不到tagcat"));
				}
				Optional<MisRelationship> optMisRelationship = tagcatRelationships.stream().filter(e -> e.getCnId() == data.tagcatId).findFirst();
				if ( optMisRelationship.isPresent() ){
					MisRelationship misRelationship = optMisRelationship.get();
					List<Tag> tags = new ArrayList<>();
					boolean isUpdate = false;
					for ( NewTagForm f : data.forms ){
						if ( f.id > 0 ){
							isUpdate = true;
						}
						Tag tag = new Tag();
						Catalog c = new Catalog();
						Lang l = Lang.parse(f.lang);
						if ( l != null ){
							if ( l == Lang.enUS ){
								c.setId(misRelationship.getEnId());
							} else if ( l == Lang.jaJP ){
								c.setId(misRelationship.getJpId());
							} else if ( l == Lang.zhCN ){
								c.setId(data.tagcatId);
							} else if ( l == Lang.zhTW ){
								c.setId(misRelationship.getTwId());
							} else if ( l == Lang.ko ){
								c.setId(misRelationship.getKrId());
							} else if ( l == Lang.th ){
								c.setId(misRelationship.getThId());
							}
						}
						c.setSiteId(siteId);
						c.setLang(l);
						tag.setCatalog(c);
						tag.setLang(l);
						tag.setId(f.id);
						tag.setName(f.tag);
						tags.add(tag);
					}
					
					if ( isUpdate ){
						tagManagement.update(tags);
						return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).syncBody(Response.create(tags, Response.STATUS_OK, ""));
					}
					
					tagManagement.create(tags);
					// 建立关系
					MisRelationship mrs = new MisRelationship();
					mrs.setSiteId(Integer.valueOf(data.siteId));
					mrs.setType(MisRelationshipType.Tag);
					tags.forEach(t -> {
						Lang l = t.getLang();
						if ( l == Lang.enUS ){
							mrs.setEnId(t.getId());
						} else if ( l == Lang.jaJP ){
							mrs.setJpId(t.getId());
						} else if ( l == Lang.ko ){
							mrs.setKrId(t.getId());
						} else if ( l == Lang.th ){
							mrs.setThId(t.getId());
						} else if ( l == Lang.zhCN ){
							mrs.setCnId(t.getId());
						} else if ( l == Lang.zhTW ){
							mrs.setTwId(t.getId());
						}
					});
					relationShipMangement.create(mrs);
					return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).syncBody(Response.create(tags, Response.STATUS_OK, ""));
				}
			}
			return ServerResponse.status(HttpStatus.BAD_REQUEST).contentType(MediaType.APPLICATION_JSON).syncBody(Response.create("", Response.STATUS_WRONGARUGS_ERROR, ""));
		});
	}

	/**
	 * 获取多语言标签
	 * @param req
	 * @return
	 */
	public Mono<ServerResponse> searchTagLang(ServerRequest req) {
		return req.bodyToMono(SearchLangTagForm.class).flatMap(form -> {
			List<Tag> tags = new ArrayList<>();
			Integer siteId = Integer.valueOf(form.siteId);
			if ( siteId != null ){
				List<MisRelationship> relationships = relationshipManagement.getRelationshipBySiteIdAndType(siteId, MisRelationshipType.Tag);
				relationships.forEach(e -> {
					if ( e.getCnId() == form.id ){
						List<Integer> ids = new ArrayList<>();
						ids.add(e.getJpId());
						ids.add(e.getEnId());
						ids.add(e.getTwId());
						ids.add(e.getThId());
						ids.add(e.getKrId());
						ids.add(e.getCnId());
						Tags ts = tagManagement.getTagsByIds(ids);
						tags.addAll(ts.getTags());
					}
				});
				return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).syncBody(Response.create(tags, Response.STATUS_OK, ""));
			}
			return ServerResponse.status(HttpStatus.BAD_REQUEST).contentType(MediaType.APPLICATION_JSON).syncBody(Response.create("", Response.STATUS_WRONGARUGS_ERROR, "参数错误"));
		});
	}

	/**
	 * 删除多语言标签
	 * @param req
	 * @return
	 */
	public Mono<ServerResponse> delTag(ServerRequest req) {
		return req.bodyToMono(DeleteLangTagForm.class).flatMap(form -> {
			if ( form.id == null || form.id.isEmpty() ){
				return ServerResponse.status(HttpStatus.BAD_REQUEST).contentType(MediaType.APPLICATION_JSON).syncBody(Response.create("", Response.STATUS_WRONGARUGS_ERROR, "id为空"));
			}
			form.id.forEach(i -> {
				tagManagement.delete(i);
			});
			return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).syncBody(Response.create("", Response.STATUS_OK, ""));
		});
	}
	/**
	 * @param req
	 * @return
	 */
	public Mono<ServerResponse> syncTags(ServerRequest req) {
		return req.bodyToMono(SyncTagForm.class).flatMap( data -> {
			// 取得书本下的所有数据
			Map<String, Map<String, Integer>> allArticles = articleManagement.getSiteIdArticleData(Integer.valueOf(data.siteId));
			MisRelationshipSync misRelationshipSync = new MisRelationshipSync();
			FolderRelationshipSync relationSync = new FolderRelationshipSync();
			data.selectItems.forEach(e -> {
				switch ( e ){
				case 1: {
					List<MisRelationship> relationships = relationshipManagement.getRelationshipBySiteIdAndType(Integer.valueOf(data.siteId), MisRelationshipType.Item);
					relationships.forEach(shs -> {
						List<FolderRelationship> folderRelationships = relationSync.getShips().get(MisRelationshipType.Item.getType());
						if ( folderRelationships == null ) {
							folderRelationships = new ArrayList<>();
							relationSync.getShips().put(MisRelationshipType.Item.getType(), folderRelationships);
						}
						FolderRelationship frs = new FolderRelationship();
						frs.cnId = shs.getCnId();
						frs.enId = shs.getEnId();
						frs.jpId = shs.getJpId();
						frs.krId = shs.getKrId();
						frs.thId = shs.getThId();
						frs.twId = shs.getTwId();
						folderRelationships.add(frs);
					});
					break;
				}
				case 2: {
					List<MisRelationship> relationships = relationshipManagement.getRelationshipBySiteIdAndType(Integer.valueOf(data.siteId), MisRelationshipType.Catalog);
					relationships.forEach(shs -> {
						List<FolderRelationship> folderRelationships = relationSync.getShips().get(MisRelationshipType.Catalog.getType());
						if ( folderRelationships == null ) {
							folderRelationships = new ArrayList<>();
							relationSync.getShips().put(MisRelationshipType.Catalog.getType(), folderRelationships);
						}
						FolderRelationship frs = new FolderRelationship();
						frs.cnId = shs.getCnId();
						frs.enId = shs.getEnId();
						frs.jpId = shs.getJpId();
						frs.krId = shs.getKrId();
						frs.thId = shs.getThId();
						frs.twId = shs.getTwId();
						folderRelationships.add(frs);
					});
					break;
				}
				case 3: {
					relationSync.getShips().put(MisRelationshipType.Hotspot.getType(), new ArrayList<>());
					break;
				}
				case 4:
					misRelationshipSync.getSyncItems().add(MisRelationshipType.RelateVideo);
					// 找到其他语言的视频
					List<Resource> videos = resManagement.getResourceByType(ResType.Video, Integer.valueOf(data.siteId));
					videos.forEach(v -> {
						switch ( v.getLanguage() ) {
						case jaJP:
							misRelationshipSync.getJpRelateVideo().put(v.getName(), Integer.valueOf(v.getUid()));
							break;
						case zhCN:
							misRelationshipSync.getCnRelateVideo().put(v.getName(), Integer.valueOf(v.getUid()));
							break;
						case enUS:
							misRelationshipSync.getEnRelateVideo().put(v.getName(), Integer.valueOf(v.getUid()));
							break;
						case zhTW:
							misRelationshipSync.getTwRelateVideo().put(v.getName(), Integer.valueOf(v.getUid()));
							break;
						case ko:
							misRelationshipSync.getKrRelateVideo().put(v.getName(), Integer.valueOf(v.getUid()));
							break;
						case th:
							misRelationshipSync.getThRelateVideo().put(v.getName(), Integer.valueOf(v.getUid()));
							break;
						}
					});
					break;
				case 5:
					misRelationshipSync.getSyncItems().add(MisRelationshipType.RelateArticle);
					break;
				case 6:
					misRelationshipSync.getSyncItems().add(MisRelationshipType.Tag);
					break;
				default:
				}
			});
			// 选择了相关视频，相关文章，tag
			if ( misRelationshipSync.getSyncItems().size() > 0 ){
				misRelationshipSync.setSiteId(Integer.valueOf(data.siteId));
				if ( data.syncType == 1 ){
					allArticles.get(Lang.zhCN.getLang()).forEach((aNo, aId) -> {
						misRelationshipSync.getCnArticles().put(aNo, aId);
					});
				} else {
					data.cnSelectArticleNos.forEach( (aNo, aId) ->{
						misRelationshipSync.getCnArticles().put(aNo, aId);					
					});			
				}
				if ( allArticles.get(Lang.jaJP.getLang()) != null ){
					allArticles.get(Lang.jaJP.getLang()).forEach((aNo, aId) -> {
						misRelationshipSync.getJpArticles().put(aNo, aId);
					});
				}
				if ( allArticles.get(Lang.enUS.getLang()) != null ){
					allArticles.get(Lang.enUS.getLang()).forEach((aNo, aId) -> {
						misRelationshipSync.getEnArticles().put(aNo, aId);
					});
				}
				if ( allArticles.get(Lang.ko.getLang()) != null ){
					allArticles.get(Lang.ko.getLang()).forEach((aNo, aId) -> {
						misRelationshipSync.getKrArticles().put(aNo, aId);
					});
				}
				if ( allArticles.get(Lang.th.getLang()) != null ){
					allArticles.get(Lang.th.getLang()).forEach((aNo, aId) -> {
						misRelationshipSync.getThArticles().put(aNo, aId);
					});
				}
				if ( allArticles.get(Lang.zhTW.getLang()) != null ){
					allArticles.get(Lang.zhTW.getLang()).forEach((aNo, aId) -> {
						misRelationshipSync.getTwArticles().put(aNo, aId);
					});
				}
				relationShipMangement.syncRelationship(misRelationshipSync);
			}
			// item, catalog, hot
			if ( relationSync.getShips().size() > 0 ) {
				relationSync.setSiteId(Integer.valueOf(data.siteId));
				if ( data.syncType == 1 ){
					allArticles.get(Lang.zhCN.getLang()).forEach((aNo, aId) -> {
						relationSync.getCnArticles().put(aNo, aId);
					});					
				} else {
					data.cnSelectArticleNos.forEach( (aNo, aId) ->{
						relationSync.getCnArticles().put(aNo, aId);					
					});
				}
				if ( allArticles.get(Lang.jaJP.getLang()) != null ){
					allArticles.get(Lang.jaJP.getLang()).forEach((aNo, aId) -> {
						relationSync.getJpArticles().put(aNo, aId);
					});
				}
				if ( allArticles.get(Lang.enUS.getLang()) != null ){
					allArticles.get(Lang.enUS.getLang()).forEach((aNo, aId) -> {
						relationSync.getEnArticles().put(aNo, aId);
					});
				}
				if ( allArticles.get(Lang.ko.getLang()) != null){
					allArticles.get(Lang.ko.getLang()).forEach((aNo, aId) -> {
						relationSync.getKrArticles().put(aNo, aId);
					});
				}
				if ( allArticles.get(Lang.th.getLang()) != null ){
					allArticles.get(Lang.th.getLang()).forEach((aNo, aId) -> {
						relationSync.getThArticles().put(aNo, aId);
					});
				}
				if ( allArticles.get(Lang.zhTW.getLang()) != null ){
					allArticles.get(Lang.zhTW.getLang()).forEach((aNo, aId) -> {
						relationSync.getTwArticles().put(aNo, aId);
					});
				}
				folderManagement.syncRelationship(relationSync);
			}
			
			return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).syncBody(Response.create("", Response.STATUS_OK, ""));
		});
		
	}
	
	/**
	 * 生成多语言大分类
	 * 
	 * @param req
	 * @return
	 */
	public Mono<ServerResponse> createItems(ServerRequest req){
		return req.bodyToMono(ItemsForm.class).flatMap( data -> {
			List<ItemForm> items = data.forms;
			List<Folder> folders = new ArrayList<>();
			items.forEach( i -> {
				Folder f = new Folder();
				f.setLanguage(Lang.parse(i.lang));
				f.setName(i.lang);
				f.setSiteId(i.siteId);
				f.setType(FolderType.Item);
				folders.add(f);
			});
			folderManagement.addFolders(folders, data.forms.get(0).siteId, FolderType.Item);
			// 添加关联
			MisRelationship misRelationship = new MisRelationship();
			misRelationship.setSiteId(data.forms.get(0).siteId);
			misRelationship.setType(MisRelationshipType.Item);
			folders.forEach( folder -> {
				switch ( folder.getLanguage() ) {
				case jaJP:
					misRelationship.setJpId(folder.getId());
					break;
				case zhCN:
					misRelationship.setCnId(folder.getId());
					break;
				case enUS:
					misRelationship.setEnId(folder.getId());
					break;
				case ko:
					misRelationship.setKrId(folder.getId());
					break;
				case th:
					misRelationship.setThId(folder.getId());
					break;
				case zhTW:
					misRelationship.setTwId(folder.getId());
					break;
				}
			});
			relationshipManagement.create(misRelationship);
			return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).syncBody(Response.create("", Response.STATUS_OK, ""));
		});
	}
	
	/**
	 * 取得Items分类
	 * 
	 * @param req
	 * @return
	 */
	public Mono<ServerResponse> queryItems(ServerRequest req){
		Optional<String> lang = req.queryParam("lang");
		Optional<String> siteId = req.queryParam("siteid");
		if ( lang != null && siteId != null && lang.isPresent() && siteId.isPresent() ) {
			FolderFilter filter = new FolderFilter();
			filter.setLanguage(lang.get());
			filter.setSiteId(Integer.valueOf(siteId.get()));
			filter.setStart(0);
			filter.setSize(999);
			Folders items = folderManagement.getFoldersByFilter(filter);
			return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).syncBody(Response.create(items.getFolders(), Response.STATUS_OK, ""));
		}
		return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).syncBody(Response.create("", Response.STATUS_WRONGARUGS_ERROR, "参数错误"));
	}
	
	
	
}
